package com.ilibrary.web.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import com.ilibrary.base.MyAjaxAction;
import com.ilibrary.base.MyBaseVO;
import com.ilibrary.constants.MyConstants;
import com.ilibrary.service.NewsService;
import com.ilibrary.web.dto.ResultDto;
import com.ilibrary.web.vo.AdminVO;
import com.ilibrary.web.vo.NewsVO;

public class DoAddNewsAction extends MyAjaxAction {

	private NewsService newsService;
	
	public NewsService getNewsService() {
		return newsService;
	}
	public void setNewsService(NewsService newsService) {
		this.newsService = newsService;
	}

	@Override
	protected JSONObject doExecute(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception {
		JSONObject jo = new JSONObject();
		String newsCategoryId = request.getParameter("newsCategoryId");
		String newsTitle = request.getParameter("newsTitle");
		String newsContent = request.getParameter("newsContent");
		
		NewsVO newsVO = new NewsVO();
		newsVO.setNewsId(0);
		newsVO.setNewsPageviews(0);
		newsVO.setNewsCategoryId(newsCategoryId);
		newsVO.setNewsTitle(newsTitle);
		newsVO.setNewsContent(newsContent);
		
		AdminVO adminVO = (AdminVO) session.getAttribute(MyConstants.ILIBRARY_LOGIN_ADMIN);
		newsVO.setCreateUser(adminVO.getAdminId());
		newsVO.setCreateDt(new Date());
		
		ResultDto<MyBaseVO> resultDto = newsService.add(newsVO);
		if(!resultDto.isSuccess()){
			jo.put("success", false);
			jo.put("msg", resultDto.getMsg());
			return jo;
		}
		jo.put("success", true);
		return jo;
	}

}
